package Function;

import VWorldGameStudio.FCPlotTouchOffEntity;
import VWorldGameStudio.FCRPlayerLink;
import VWorldGameStudio.touzi.FCTouZiProject;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Vector;

import static FCRendering.FCRMaster.*;

/***
 * 用于检测剧情是否触发 根据各自剧情的触发条件检测判断
 */
public class FCPlotDetect {
	private Vector<String> plotid;
	private Vector<String> plottouchoff;
	private HashMap<String, Boolean> plotisend;
	private HashMap<String, FCPlotTouchOffEntity> touchOffEntityHashMap = new HashMap<>();

	public FCPlotDetect(Vector<String> plotid, Vector<String> plottouchoff, HashMap<String, Boolean> plotisend) {
		this.plotid = plotid;
		this.plottouchoff = plottouchoff;
		this.plotisend = plotisend;
		Gson gson = new Gson();
		for(int i = 0; i<plotid.size(); i++){
			FCPlotTouchOffEntity entity = gson.fromJson(plottouchoff.get(i), FCPlotTouchOffEntity.class);
			touchOffEntityHashMap.put(plotid.get(i), entity);
			System.out.println("剧情："+plotid.get(i)+" 触发条件"+gson.toJson(entity));
		}
	}

	/** 
	 * 检测事件触发,判断条件是否符合。符合 则返回事件内容
	 * @return string
	 * <touchoff noTouchOff=1> 表示不跑这里判断，是主动设置触发的，比如死亡见明奇重复的复活剧情
	 * <touchoff finishEventId=testbattledog> 表示FCeventId永久完成了就触发这个剧情一次，见gaveSave.fcEventHasHanppen
	 *触发剧情的几大类形，事件完成组合，队伍里有什么人，人物状态（血量，等级，金钱，在什么地图，或具体的什么位置，拥有什么物品，拥有什么坦克）的组合
	 *事件完成组合放在gaveSave.fcEventHasHanppen
	 * 实现在FCPlotTouchOffEntity
	 */
	public String isTriggerPlotOneplayer() {
		for (int i = 0; i < plotid.size(); i++) {
			if (plotisend.get(plotid.get(i)) == null || !plotisend.get(plotid.get(i))) {
				FCPlotTouchOffEntity touchOffEntity = touchOffEntityHashMap.get(plotid.get(i));
				//TODO 条件要完全满足FCPlotTouchOffEntity体就触发
				if(touchOffEntity.getNoTouchOff() == 1){//跳过查这个
					continue;
				}

				if(touchOffEntity.getLevel() != -1){
					//TODO 要判断level，条件不满足就继续查
					if(touchOffEntity.getLevel() != player1.getLevel()){
						continue;
					}
				}
				if(touchOffEntity.getBlood() != -1){
					//TODO 要判断，条件不满足就继续查
					if(touchOffEntity.getBlood() != player1.getBlood()){
						continue;
					}
				}
				if(touchOffEntity.getMoney() != -1){
					if(touchOffEntity.getMoney() != player1.getMoney()){
						continue;
					}
				}
				if(touchOffEntity.getMapName() != null){
					if(!touchOffEntity.getMapName().equals(player1.getMapname())){
						continue;
					}
				}
				if(touchOffEntity.getMapX() != -1){
					if(touchOffEntity.getMapX() != player1.getPlayer1x()){
						continue;
					}
				}
				if(touchOffEntity.getMapY() != -1){
					if(touchOffEntity.getMapY() != player1.getPlayer1y()){
						continue;
					}
				}
				if(touchOffEntity.getHasItems() != null){
					boolean findAllItem = false;
					for(String iteNa: touchOffEntity.getHasItems()){
						if(iteNa == null) continue;
						findAllItem = false;
						for(int n = 0;n<player1.getKnapsack().length; n++){
							if(iteNa.equals(player1.getKnapsack()[n])){
								findAllItem = true;
								break;
							}
						}
						if(!findAllItem){
							break;
						}
					}
					if(!findAllItem){
						continue;
					}
				}
				if(touchOffEntity.getDrivingTank() != null){
					if(!touchOffEntity.getDrivingTank().equals(player1.getPlayertank1())){
						continue;
					}
				}
				if(touchOffEntity.getNpcWhoInTeam() != null){
					boolean findAllNpc = false;
					for(String npc: touchOffEntity.getNpcWhoInTeam()){
						if(npc == null) continue;
						findAllNpc = false;
						for(FCRPlayerLink link: FCMutilPlayCache.getTeamMembers()){
							if(link.curPlayerName.equals(npc)){
								findAllNpc = true;
								break;
							}
						}
						if(!findAllNpc){
							break;
						}
					}
					if(!findAllNpc){
						continue;
					}
				}
				if(touchOffEntity.getEventHasHappens() != null){
					boolean findAllEvents = false;
					for(String eventId: touchOffEntity.getEventHasHappens()){
						if(eventId == null) continue;
						findAllEvents = false;
						for(String eId: gameSave.getFcEventHasHanppen()){
							if(eId.equals(eventId)){
								findAllEvents = true;
								break;
							}
						}
						if(!findAllEvents){
							break;
						}
					}
					if(!findAllEvents){
						continue;
					}
				}

				if(touchOffEntity.getPlotHasHappens() != null){
					boolean findAllPlots = true;
					for(String plot: touchOffEntity.getPlotHasHappens()){
						if(plot == null) continue;
						Boolean hasPlot = plotisend.get(plot);
						if(hasPlot == null || !hasPlot){
							//未发生该剧情
							findAllPlots = false;
							break;
						}
					}
					if(!findAllPlots){
						continue;
					}
				}

				if(touchOffEntity.getMonsterHasKill() != null){
					boolean findAllBoss = true;
					for(String boss: touchOffEntity.getMonsterHasKill()){
						if(boss == null) continue;
						if(gameSave.getFcMonsterKillRecordMap().get(boss) == null){
							findAllBoss = false;//未打过这个boss
							break;
						}
					}
					if(!findAllBoss){
						continue;
					}
				}
				if(touchOffEntity.getTouZiActionName() != null){
					FCTouZiProject project = null;
					for(FCTouZiProject project1: gameSave.getTouZiProjects()){
						if(touchOffEntity.getTouZiActionName().equals(project1.projectEnName)){
							project = project1;
							break;
						}
					}
					if(project != null && touchOffEntity.getTouZiMoney() <= project.touZiMoney){
						//通过剧情
					}else {
						continue;
					}
				}
				if(touchOffEntity.getEndSayEventId() != null){
					if(!touchOffEntity.getEndSayEventId().equals(fcv.getLastEndSayEventId())){
						continue;
					}
				}

				if(touchOffEntity.getMapxs() != null){
					boolean find = false;
					for(int mm = 0; mm < touchOffEntity.getMapxs().size(); mm++){
						if(player1.getPlayer1x() == touchOffEntity.getMapxs().get(mm)
						 && player1.getPlayer1y() == touchOffEntity.getMapys().get(mm)){
							find = true;
							break;
						}
					}
					if(!find){
						continue;
					}
				}
				if(touchOffEntity.getTriggerCmd() != -1){
					if(!FCPlotTouchOffEntity.isTriggetCmdFix(touchOffEntity.getTriggerCmd())){
						continue;
					}
				}
				if(touchOffEntity.getTouchOffKey() != null){
					String[] vl = touchOffEntity.getTouchOffKey().split(":");
					if(!vl[1].equals(gameSave.getPlotTouchOffVal(vl[0]))){
						continue;
					}
				}
				if(touchOffEntity.getEndSayNum() != -1){
					if(touchOffEntity.getEndSayNum() != fcv.getLastEndSayNum()){
						continue;
					}
				}
				System.out.println("触发剧情Id "+plotid.get(i));
				return plotid.get(i);
			}
		}
		return null;
	}


	public void setplayer(HashMap<String, Boolean> plotisend) {
		this.plotisend = plotisend;
	}
}
